package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet2
 */
@WebServlet("/LoginServlet2")
public class LoginServlet2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");    //设置输入内容的编码方式
		
		response.setContentType("text/html;charset=UTF-8");   //设置为html编码格式
		
		String username=request.getParameter("username");
		
		System.out.println(username);
		
		String password=request.getParameter("password");
		
		System.out.println(password);
		
//		request.setAttribute("username", username);
//		request.setAttribute("password", password);
		if(username.equals("user0")) {
			Cookie cookie=new Cookie("username", username); //会话cookie
			cookie.setMaxAge(60*10); //永不过期 -1  删除 0  大于0(秒)存磁盘  不设置会话或临时Cookie
//			cookie.setHttpOnly(true);//设置了HttpOnly属性，那么通过js脚本将无法读取到cookie信息，这样能有效的防止XSS攻击，
//			cookie.setPath("/path");//将服务器的文件目录作为Cookie的适用对象（若不指定则默认为文档的文件目录）
			cookie.setDomain("");//作为Cookie适用对象的域名（若不指定则默认为创建Cookie的服务器域名）
			response.addCookie(cookie);
			response.sendRedirect("IndexServlet2");
		}else {
			response.sendRedirect("login2.html");
		}
	}

}
